#!/bin/bash 

#set argument
USER_NAME="linkcrm"
APP_PORT="8025"
SERVER_NAME="xxljob"
JAR_FILE_NAME="xxl-job-admin-2.0.1.jar"
APP_PATH="/xxl-job-admin"
DATABASE_URL="jdbc:mysql://192.168.0.45:3306/linkadmin?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&useAffectedRows=true"
DATABASE_USERNAME="linkcrm"
DATABASE_PASSWORD="8Bn,l!csllZ"
LOGIN_NAME="admin"
LOGIN_PASSWORD="123456"


CUR_DIR=`pwd`
PAR_DIR=`echo ${CUR_DIR%/*}`
JAR_FILE=${CUR_DIR}"/"${JAR_FILE_NAME}
SOURCE_JAR_FILE=${PAR_DIR}"/jars/"${JAR_FILE_NAME}
BACKUP_JAR_PATH=${CUR_DIR}"/backup"

#about jvm
JVM_ARG="-XX:+DisableExplicitGC \
	 -XX:+HeapDumpOnOutOfMemoryError \
	 -XX:HeapDumpPath=${CUR_DIR}/logs/ \
	 -XX:MetaspaceSize=256m \
	 -XX:YoungGenerationSizeIncrement=50 \
	 -Xms1024m \
	 -Xmx1024m \
	 -XX:NewSize=600m"

GC_LOG_ARG="-XX:+PrintGCDetails \
            -XX:+PrintGCTimeStamps \
            -XX:+PrintGCDateStamps \
            -Xloggc:${CUR_DIR}/logs/gc.log \
            -XX:+PrintHeapAtGC \
            -XX:+PrintTenuringDistribution"

#start server function
function start (){
    echo "========> start `echo $SERVER_NAME`:`echo $APP_PORT` server!"
    if [ "$APP_PORT" = "" ]; then
        echo "请设置服务运行端口！！！"
        exit 0
    fi

    if [ "$SHUTDOWN_PORT" = "$APP_PORT" ]; then
        echo "服务运行端口和停服务的端口不能相同！！！"
        exit 0
    fi

    #打印gc日志相关参数
    set_gc_log $1

    if [ ! -f $JAR_FILE ]; then
        echo "have no file $JAR_FILE ,so you cant start the server `echo $SERVER_NAME`:`echo $APP_PORT`"
        exit 1
    fi

    pid=`ps -ef | grep -v "grep" | grep "server.port=$APP_PORT" | awk '{print $2}'`
    if [ "$pid" != "" ];then
        echo "`echo $SERVER_NAME`:`echo $APP_PORT` is running, pid is $pid, Please stop the service before starting"
        exit 1
    fi

    echo "start `echo $SERVER_NAME`:`echo $APP_PORT`"
    nohup java -jar `echo $JVM_ARG` `echo $JAR_FILE` \
            --spring.datasource.url=`echo $DATABASE_URL` --spring.datasource.username=`echo $DATABASE_USERNAME` \
            --spring.datasource.password=`echo $DATABASE_PASSWORD` --xxl.job.login.username=`echo $LOGIN_NAME` \
            --xxl.job.login.password=`echo $LOGIN_PASSWORD` --server.port=`echo $APP_PORT` --server.context-path=`echo $APP_PATH` \
            > /dev/null  2>&1 &    

}

#stop server function
function stop (){
    echo "=========>stop `echo $SERVER_NAME`:`echo $APP_PORT`!"
    pid=$(ps -ef | grep -v "grep" | grep "server.port=`echo $APP_PORT`" | awk '{print $2}')
    if [ "$pid" != "" ];then
        #curl -X POST  localhost:`echo $`/shutdown
        echo "执行停止命令，端口为：`echo $pid`！"
        #curl -X POST http://127.0.0.1:`echo $SHUTDOWN_PORT`/admin/shut
        kill -9 `echo $pid`
    fi

    echo ""
    num=0
    maxnum=180
    b='#'
    pid=$(ps -ef | grep -v "grep" | grep "server.port=`echo $APP_PORT`" | awk '{print $2}')
    while [ "$pid" != "" ]
    do
        if [ "$pid" != "" ];then
            if [ $num -eq $maxnum ];then
                echo "停服务失败，请检查服务运行情况！"
                exit 0
            fi
            sleep 1
            let "num += 1"
            printf "服务正在停止:[%-180s]%d%%\r" $b $num
            b=#$b    
            pid=$(ps -ef | grep -v "grep" | grep "server.port=`echo $APP_PORT`" | awk '{print $2}')
        fi
    done
    echo
    echo "服务已经停止！！"
}

#get status function
function status (){
    pid=$(ps -ef | grep -v 'grep' | grep "server.port=`echo $APP_PORT`" | awk '{printf $2 " "}')
    #echo "$pid"
    if [ "$pid" != "" ]; then
        echo "`echo $SERVER_NAME`:`echo $APP_PORT` is running,pid is $pid"
    else
        echo "`echo $SERVER_NAME`:`echo $APP_PORT` is stopped"
    fi   
}

#tishi function 
function usage(){
   echo "The command format is : $0 {start|stop|restart|status|startnoc|restartnoc|recovery}{gclog}"
   exit 0
}


#set gc flag
function set_gc_log(){
    if [ "$1" = "gclog" ]; then 
        JVM_ARG="${JVM_ARG} ${GC_LOG_ARG}"
    fi
    echo "JVM_ARG:"`echo $JVM_ARG`
}


#Verification user 
if [ $(whoami) != $USER_NAME ]; then
    echo "请使用用户： $USER_NAME 执行该命令!"
    exit 0
fi  

case "$1" in
    start)
        start $2
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start $2
        ;;
    reload)
        echo "no use!"
        ;;
    status)
        status
        ;;
    startnoc)
        start $2
        ;;
    restartnoc)
        stop
        start $2
        ;;
    recovery)
        stop
        start $2
        ;;
    test)
	echo "CUR_DIR:"`echo $CUR_DIR`
	echo "PAR_DIR:"`echo $PAR_DIR`
	echo "JAR_FILE:"`echo $JAR_FILE`
	echo "APP_PORT:"`echo $APP_PORT`
	echo "SERVER_NAME:"`echo $SERVER_NAME`
	echo "APP_PATH:"`echo $APP_PATH`
	echo "SOURCE_JAR_FILE:"`echo $SOURCE_JAR_FILE`
	echo "ACTIVE_PROFILE:"`echo $ACTIVE_PROFILE`
	echo "BACKUP_JAR_PATH:"`echo $BACKUP_JAR_PATH`
	echo "JAR_FILE_NAME:"`echo $JAR_FILE_NAME`
	echo "USER_NAME:"`echo $USER_NAME`
	echo "SHUTDOWN_PORT:"`echo $SHUTDOWN_PORT`
    echo "JVM_ARG:"`echo $JVM_ARG`
    echo "GC_LOG_ARG:"`echo $GC_LOG_ARG`
	;;
    *)
      usage
      ;;
esac

exit 0

